package demiBoard;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Set;

public class DemiBoardFunction {
	//public static SimpleDataFormat format;

	public static String longTimeToFormat(long timeData,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String returnVal=null;
		
		Date timeVal=new Date(timeData);
		returnVal=sdf.format(timeVal).toString();
		
		return returnVal;
	}
	public static String getBoardTag(ArrayList board){
		StringBuffer returnTag=new StringBuffer();
		StringBuffer replyTag=new StringBuffer();
		
		returnTag.append("<ul class='ulTable'>\n");
		for(int i=0;i<board.size();i++)
		{
			HashMap tempItem =(HashMap)board.get(i);
			String postId=(String)tempItem.get("cPostId");
			String postName=(String)tempItem.get("cPostName");
			//String postData=(String)tempItem.get("cPostData");
			String postDate=longTimeToFormat(Long.parseLong((String)tempItem.get("cPostDate")),"yyyy.MM.dd HH.mm");
			String postMother=(String)tempItem.get("cMother");
			int cDepth=Integer.parseInt((String)tempItem.get("cDepth"));
			
			String aTagAttr=" href='#' onclick='readPost(\""+postId+"\");' ";
			
			
			if(postMother.equals("0")){
				returnTag.append("<li>");
			}
			else{
				returnTag.append("<li class='replyLi'>");
			}
			//답글 
			for(int j=0;j<cDepth;j++){
				if(j==cDepth-1){
					replyTag.append("<span class='replyPadLast'>=</span>");
				}
				else{
					replyTag.append("<span class='replyPad'>=</span>");
				}
			}

			returnTag.append("<div class='cPostId'>"+postId+"</div>"+
			"<div class='cPostName'>"+replyTag.toString()+"<a"+aTagAttr+">"+postName+"</a></div>"+
			//"<div ='cPostData'>"+postData+"</div>"+
			"<div class='cPostDate'>"+postDate+"</div>"+
			//"<div class='cMother'>"+postMother+"</div>"+
			//"<div class='cDepth'>"+cDepth+"</div>"+
			"</li>\n");
			replyTag.delete(0,replyTag.length());
		}
		
		returnTag.append("</ul><div class='clear'></div>");
		return returnTag.toString();
	}
		
	public static String getArrayListToHTML(ArrayList list,String ulClassName){
		StringBuffer returnTag=new StringBuffer();
		Object[] keys=null;
		if(list.size()==0){
			return "nolist";
		}
		returnTag.append("<ul class='"+ulClassName+"'>\n");
		
		for(int i=0;i<list.size();i++)
		{
			returnTag.append("<li>\n");
			////////////////////////
			HashMap map=(HashMap)list.get(i);
			if(i==0){
				Set key=map.keySet();
				keys=key.toArray();
			}
			for(int j=0;j<keys.length;j++){
				returnTag.append("<div class='"+keys[j]+"'>" +
						map.get(keys[j])+"</div>");
			}
			////////////////////////
			returnTag.append("</li>\n");
		}
		returnTag.append("</ul>");
		return returnTag.toString();
		
	}
	/*보드 하단 네비게이션*/
	public static String getBoardNaviTag(int numOfPage,int boardPerPage,int curPage){
		StringBuffer returnTag=new StringBuffer();
		
		System.out.println(numOfPage+":"+boardPerPage+":"+curPage);
		int maxNaviPage=(int)Math.ceil((double)numOfPage/boardPerPage);
		int curNaviPage=(int)Math.ceil((double)curPage/boardPerPage);
		int startPage=((curNaviPage-1)*boardPerPage)+1;
		int endPage=startPage+boardPerPage;
		System.out.println("startPage:"+startPage);
		System.out.println("endPage:"+endPage);
		System.out.println("maxNaviPage:"+maxNaviPage);
		System.out.println("curNaviPage:"+curNaviPage);

		
		if(endPage>numOfPage){
			endPage=numOfPage+1;
		}
		returnTag.append("<span class='boardPagePrev'>");
		if(curNaviPage>1){
			returnTag.append("<input type='button' value='<' onclick='goPrevPagePage()'/>");
		}
		returnTag.append("</span>");
		
		for(int i=startPage;i<endPage;i++)
		{
			returnTag.append("<span class='boardPageList'><a href='#' onclick='goTargetPage("+i+")'>");
			returnTag.append(i);
			returnTag.append("</a></span>");
		}
		returnTag.append("<span class='boardPageNext'>");
		if(curNaviPage!=maxNaviPage){
			returnTag.append("<input type='button' value='>' onclick='goNextPagePage()'/>");
		}
		returnTag.append("</span>");
	
		return returnTag.toString();
	}
	public static String getBoardMenuTag(ArrayList boardMenuList){
		StringBuffer returnTag=new StringBuffer();
		HashMap tempMap;
		returnTag.append("<ul>\n");
		for(int i=0;i<boardMenuList.size();i++)
		{
			tempMap=(HashMap)boardMenuList.get(i);
			returnTag.append("<li><a href='#' onclick='goBoard("+tempMap.get("cBoardId")+");'>"+tempMap.get("cBoardName")+"</a></li>");
		}	
		returnTag.append("</ul>\n");
		return returnTag.toString();
	}
	public static String getBoardMenuStatusTag(ArrayList boardMenuList){
		StringBuffer returnTag=new StringBuffer();
		HashMap tempMap;
		returnTag.append("<ul class='ulTable'>\n");
		for(int i=0;i<boardMenuList.size();i++)
		{
			tempMap=(HashMap)boardMenuList.get(i);
			returnTag.append("<li>");
			returnTag.append("<div>"+tempMap.get("cBoardId")+"</div>");
			returnTag.append("<div><a href='#' onclick='goBoard("+tempMap.get("cBoardId")+");'>"+tempMap.get("cBoardName")+"</a></div>");
			returnTag.append("<div>"+tempMap.get("cBoardPerPostNum")+"</div>");
			returnTag.append("<div>"+tempMap.get("cBoardPerPageNum")+"</div>");
			returnTag.append("</li>");
		}	
		returnTag.append("</ul><div class='clear'></div>\n");
		return returnTag.toString();
	}
	
	
	
	
	
}
